<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/12/28
 * Time: 20:13
 */

namespace app\index\model;


use think\Model;

class Baseinsulin extends Model
{

    /*
     * 获得对的胰岛素基础总量
     * @param int $uid
     * @param string $sdatetime  查询记录的日期
     * @return model|false $bi
     * */
    public function getbaseinsulin($uid,$sdatetime=''){
        
        if (empty($sdatetime)){
            
            $sdatetime = time();
        }
        $sdatetime = strtotime('+1day',$sdatetime);//改变当天也要起效
        $bi = $this->where('uid',$uid)->where('update_time','<',$sdatetime)->order('update_time desc')->find();
        if (empty($bi)){
            $bi = $this->where('uid',$uid)->order('update_time asc')->find();
            return $bi;
        }else{
            return $bi;
        }
    }


    /*
     * 插入胰岛素基础总量的记录
     * @param int $uid
     * @param string $baseinsulin  基础率总量
     * @param string $sdatetime  插入时间
     * */
    public function insertbaseinsulin($uid,$baseinsulin,$update_time){
        $data  = array(
            'uid' => $uid,
            'baseinsulin' => $baseinsulin,
            'update_time' => $update_time
        );
        $sdatetime = strtotime('+1day',time());//改变当天也要起效

        $bi = $this->where('uid',$uid)->where('update_time','<',$sdatetime)->order('update_time desc')->find();
        
        if ($bi['baseinsulin'] == $baseinsulin){
            //没有改变的话，不需要修改
            return false;
        } 
        
        $r = $this->isUpdate(false)->save($data);
        if ($r!=false){
            return false;
        }

        return $r;



    }


}